package com.ruoyi.quartz.task;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.base.BaseEntity;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.WdyBuyProduct;
import com.ruoyi.system.domain.WdyVcRelease;
import com.ruoyi.system.service.IWdyBuyProductService;
import com.ruoyi.system.service.IWdyVcReleaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Component("releaseTask")
public class ReleaseTask {

    private static final Logger logger = LoggerFactory.getLogger(ReleaseTask.class);
    @Autowired
    private IWdyBuyProductService wdyBuyProductService;

    @Autowired
    private IWdyVcReleaseService wdyVcReleaseService;

    public void releaseVc() {
        releaseVcByDate(null);
    }

    public void releaseVcByDate(String curDateStr) {
        QueryWrapper<WdyBuyProduct> buyProductQueryWrapper = new QueryWrapper<>();
        buyProductQueryWrapper.gt("freeze_vc_amount", 0);
        buyProductQueryWrapper.eq("active_state", BaseEntity.YES);
        List<WdyBuyProduct> list = wdyBuyProductService.list(buyProductQueryWrapper);

        logger.info("释放记录数量：{}", list.size());
        if(CollectionUtils.isEmpty(list)) {
            return;
        }
        if(StringUtils.isEmpty(curDateStr)) {
            curDateStr = DateUtils.getDate();
        }
        Date curDate = DateUtils.parseDate(curDateStr);
//        Map<String, BigDecimal> incrAmountMap = new HashMap<>();
//        StopWatch watch = new StopWatch();
        for(WdyBuyProduct buyProduct : list) {
            QueryWrapper<WdyVcRelease> releaseQuery = new QueryWrapper<>();
            releaseQuery.eq("user_id", buyProduct.getUserId());
            releaseQuery.eq("txn_id", buyProduct.getId());
            releaseQuery.ge("create_time", curDateStr + " 00:00:00");
            releaseQuery.le("create_time", curDateStr + " 23:59:59");
            if(wdyVcReleaseService.count(releaseQuery) > 0) {
                //logger.info("已释放:{}", buyProduct.getId());
                continue;
            }
            for(int i = 0; i < 100; i++){
                try {
                    //watch.start();
                    buyProduct = wdyVcReleaseService.release(buyProduct, curDate);
                    //watch.stop();
                    //logger.info("释放耗时{}ms", watch.getLastTaskTimeMillis());
//                    if(buyProduct.getFreezeVcAmount().compareTo(BigDecimal.ZERO) <= 0 && buyProduct.getReleaseAmount().compareTo(buyProduct.getReleaseBase()) > 0) {
//                        BigDecimal incrAmount = buyProduct.getReleaseAmount().subtract(buyProduct.getReleaseBase());
//                        if(incrAmountMap.containsKey(buyProduct.getUserId())) {
//                            incrAmountMap.put(buyProduct.getUserId(), incrAmount.add(incrAmountMap.get(buyProduct.getUserId())));
//                        } else {
//                            incrAmountMap.put(buyProduct.getUserId(), incrAmount);
//                        }
//                    }
                    break;
                } catch (Exception e) {
                    //watch.stop();
                    logger.error("释放异常:"+JSON.toJSON(buyProduct), e);
                }
            }
        }
//        if(incrAmountMap.size() > 0) {
//            for(Map.Entry<String, BigDecimal> entry : incrAmountMap.entrySet()) {
//                wdyBuyProductService.incrReleaseAmount(entry.getKey(), entry.getValue());
//            }
//        }
    }
}
